//
//  COFoodViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/20.
//

import UIKit
import JXSegmentedView
import MJRefresh

class COFoodViewController: COBaseViewController {
    override var navShow: Bool {
        false
    }
    private var page = 1
    var contents: [FoodType] = []
    var array: [TypeModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
       
        contents = [.italy, .france, .usa, .russia, .britain, .thailand, .japan, .other]
        self.collectionView.reloadData()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.getData()
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 160, height: 160)
        layout.sectionInset = .init(top: 44, left: 20, bottom: 0, right: 20)
        layout.minimumLineSpacing = 20
        layout.minimumInteritemSpacing = (SCREENWIDTH-40-320)
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.delegate = self
        cv.dataSource = self
        cv.register(COFoodCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        cv.backgroundColor = .rgba(17, 7, 41)
        
        return cv
    }()

}

extension COFoodViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.contents[indexPath.row]
        let vc = COQAListViewController()
        vc.title = model.name
        vc.qaId = model.rawValue
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension COFoodViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        contents.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! COFoodCollectionViewCell
        let item = contents[indexPath.row]
        cell.model = item
        cell.totalLabel.text = "0 in total"
        for model in array {
            if model.type == item.rawValue {
                cell.totalLabel.text = "\(model.count) in total"
                break
            }
        }
        return cell
    }
    
    private func getData() {
        self.view.makeToastActivity(.center)
        CONetwork.sendRequest(provider: homeProvider, target: .food) { text in
            self.view.hideToastActivity()
            guard let array = text.array else {
                return
            }
            
            self.array = array.map({ item in
                let model = TypeModel.deserialize(from: item)!
                return model
            })
            
            self.collectionView.reloadData()
        } failedBlock: { _, text in
            self.view.hideToastActivity()
            self.view.makeToast(text, position: .center)
        }
    }
}

extension COFoodViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return self.view
    }
}

import HandyJSON
class TypeModel: HandyJSON {
    var count = 0
    var type = 0
    required init() {
    }
}
